﻿var CalendarObject = function () {
    /*
        The object that manages the Dynarch calendars
    */

    this.intMaxDate = null;
    this.intMinDate = null;
    this.strFrom = 'From';
    this.strTo = 'To';
    this.dateFormat = '%d/%m/%Y';
    this.selectRangeCall = null;
    this.isEnabled = true;

    this.hasCustomPeriod = false;


    /********************************************************************************
	These variables are commented out, because they need to be added using prototype.
	---------------------------------------------------------------------------------
	
	this.inactiveClassName = '';
	this.activeClassName = '';
	this.cssFileURL = '';
		
	********************************************************************************/

    var This = this
		, From = {
		    Cal: null
			, input: null
			, isSelected: false
		}
		, To = {
		    Cal: null
			, input: null
			, isSelected: false
		}
		, button = null
		, activePeriodClassName = 'ActivePeriodButton'
    ;

    this.clear = function () {
        button.removeClass(activePeriodClassName);

        From.input[0].value = this.strFrom;
        To.input[0].value = this.strTo;

        if (From.isSelected)
            From.Cal.selection.unselect(From.Cal.selection.getDates()[0]);
        if (To.isSelected)
            To.Cal.selection.unselect(To.Cal.selection.getDates()[0]);

        this.hasCustomPeriod = false;

        From.isSelected = false;
        To.isSelected = false;

        From.input.addClass(this.activeClassName).addClass(this.inactiveClassName);
        To.input.addClass(this.activeClassName).addClass(this.inactiveClassName);
    };

    this.load = function () {

        From.input = $('#FromCalendarInput');
        To.input = $('#ToCalendarInput');
        button = $('#SearchButton').mousedown(search);

        From.input[0].value = this.strFrom;
        To.input[0].value = this.strTo;

        From.input.addClass(this.inactiveClassName);
        To.input.addClass(this.inactiveClassName);

        var disabledCall;
        if (this.isEnabled) {
            disabledCall = function () {
                return false;
            };
        } else {
            disabledCall = function () {
                return true;
            };
        }

        From.Cal = new Calendar({
            inputField: 'FromCalendarInput'
			, trigger: 'FromCalendarImage'
			, max: this.intMaxDate
			, min: this.intMinDate
			, dateFormat: this.dateFormat
			, onSelect: function () {
			    From.Cal.hide();
			    To.Cal.hide();
			    if (!This.isEnabled)
			        return;
			    From.isSelected = true;
			    button.addClass(activePeriodClassName);
			}
			, disabled: disabledCall
        });

        To.Cal = new Calendar({
            inputField: 'ToCalendarInput'
			, trigger: 'ToCalendarImage'
			, max: this.intMaxDate
			, min: this.intMinDate
			, dateFormat: this.dateFormat
			, onSelect: function () {
			    From.Cal.hide();
			    To.Cal.hide();

			    if (!This.isEnabled)
			        return;
			    To.isSelected = true;
			    button.addClass(activePeriodClassName);
			}
			, disabled: disabledCall
        });

        Utility.addCssFile(This.cssFileURL);
    };

    this.getCurrentDates = function () {
        /*
            Returns the currently selected dates, if the from date is missing then the min date is used and if the To date is missing then the max date is used
        */
        var ret = {
            dFrom: !From.isSelected ? Calendar.intToDate(This.intMinDate) : From.Cal.selection.getDates()[0],
            dTo: !To.isSelected ? Calendar.intToDate(This.intMaxDate) : To.Cal.selection.getDates()[0]
        }
        return ret;
    };

    this.getMaxDate = function () {
        /*
            Returns the maximum available date as a JS Date object
        */
        return Calendar.intToDate(This.intMaxDate);
    };

    function search() {
        if (!From.isSelected && !To.isSelected || !This.isEnabled)
            return;

        var Dates = This.getCurrentDates();
        button.addClass(activePeriodClassName);

        if (From.isSelected)
            From.input.removeClass(This.inactiveClassName).addClass(This.activeClassName);
        if (To.isSelected)
            To.input.removeClass(This.inactiveClassName).addClass(This.activeClassName);

        This.hasCustomPeriod = true;

        if (typeof This.selectRangeCall == 'function') {
            This.selectRangeCall(Dates.dFrom, Dates.dTo);
        }
    }

    function _init() {
        if (typeof This.inactiveClassName != 'string')
            This.inactiveClassName = 'InactiveInputBox';
        if (typeof This.activeClassName != 'string')
            This.activeClassName = 'ActiveInputBox';
        if (typeof This.cssFileURL != 'string') {
            This.cssFileURL = '';
        }
    }

    _init();
};